import RestFetch from '@/utils/request/RestFetch'
import { ElLoadingService } from 'element-plus'
import type { ILoadingInstance } from 'element-plus'
import type { RequestOptions } from '@/utils/request/RestFetch'
import type { FeatureCollection } from 'geojson'
const restFetch = new RestFetch({
  baseURL: process.env.VITE_ARCGIS_API
})

interface InitOptions {
  msg?: boolean;
  loading?: boolean;
}

async function request<T = FeatureCollection> (options: RequestOptions, init?: InitOptions): Promise<T> {
  const loading = !!init?.loading
  let loadingService: ILoadingInstance | null = null
  if (loading) {
    loadingService = ElLoadingService({})
  }
  const defaultOptions = {
    where: '1=1',
    f: 'geojson',
    outFields: '*',
    returnGeometry: true
  }

  if (options.method === 'GET') {
    options.params = {
      ...defaultOptions,
      ...options.params
    }
  } else {
    options.data = {
      ...defaultOptions,
      ...options.data
    }
  }
  const res = await restFetch.request<T>(options)

  loadingService?.close()
  return res
}

export default request
